﻿package tv.colmeia.game {
	/**
	* ...
	* @author Martin Fabichak @ colmeia.tv
	*/
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	import org.cove.ape.AbstractParticle;
	import org.cove.ape.CollisionEvent;
	import org.cove.ape.Group;
	import org.cove.ape.RectangleParticle;
	import org.cove.ape.Vector;
	import org.cove.ape.VectorForce;
	import tv.colmeia.game.assets.Ambulancia;
	import tv.colmeia.game.assets.Enfermeiros;
	import tv.colmeia.game.assets.events.GameSocketEvent;
	import tv.colmeia.game.assets.Floor;
	import tv.colmeia.game.assets.sprites.EnfermeiroSprite;
	import tv.colmeia.game.assets.sprites.AmbulanciaSprite;
	import tv.colmeia.game.assets.sprites.VitimaSprite;
	import tv.colmeia.game.assets.Vitima;
	
	import org.cove.ape.APEngine;
	import org.cove.ape.CollisionEvent;
	
	import flash.ui.Keyboard;
	
	import tv.colmeia.game.comm.GameSocket;
	import tv.colmeia.game.assets.events.GameSocketEvent
	public class GameView extends Sprite{
		
		//obstaculo
		public var obs_1:Sprite;
		public var obs_2:Sprite;
		public var obs_3:Sprite;
		public var obs_4:Sprite;
		
		//
		public var ponto:Sprite;
		public var pontuacao:TextField;
		
		//assets
		public var enfermeiros:EnfermeiroSprite;
		public var ambulancia:AmbulanciaSprite;
		public var fundo_mc:Sprite;
		
		//group
		private var _group			:Group;
		private var _enfermeiro		:RectangleParticle;
		private var _ambulancia		:RectangleParticle;
		private var _floor			:Floor;
		
		//game
		private var peopleTimer:Timer;
		private var _gameSocket:GameSocket;
		public function GameView() {
			
			ponto.visible = false;
			
			APEngine.init();
			APEngine.container = fundo_mc;
			APEngine.damping = 0.98;
			APEngine.addForce(new VectorForce(false, 0,2));
			
			addGroup();
			
			peopleTimer = new Timer(2000);
			peopleTimer.addEventListener(TimerEvent.TIMER, addVictim);
			peopleTimer.start();
			
			addEventListener(Event.ENTER_FRAME, run);
			
			_gameSocket = new GameSocket("192.168.2.71", 8484);
			_gameSocket.addEventListener(GameSocketEvent.GAME_EVENT, key);
			//stage.addEventListener(KeyboardEvent.KEY_DOWN, key);
		}
		
		private function addGroup():void {
			_group = new Group(true);
			
			_enfermeiro = new Enfermeiros(enfermeiros);
			_ambulancia = new Ambulancia(ambulancia, ponto);
			_ambulancia.addEventListener(Event.COMPLETE, addPonto);
			_floor 		= new Floor();
			_floor.addEventListener(CollisionEvent.COLLIDE, floorCollide);
			
			_group.addParticle(_enfermeiro);
			_group.addParticle(_ambulancia);
			_group.addParticle(_floor);
			
			APEngine.addGroup(_group);
		}
		private function addVictim(e:TimerEvent):void {
			var v:Vitima = new Vitima(new VitimaSprite(peopleTimer.currentCount));
			_group.addParticle(v);
		}
		private function floorCollide(e:CollisionEvent):void {
			if (e.collidingItem.sprite.name != "enfermeiros" && e.collidingItem.sprite.name != "ambulancia") {
				_group.removeParticle(e.collidingItem as AbstractParticle);
				pontuacao.text = String(int(pontuacao.text) - 5);
			}
		}
		private function addPonto(e:Event):void {
			pontuacao.text = String(int(pontuacao.text) + 15);
		}
		private function run(e:Event):void {
			APEngine.paint();
			APEngine.step();
		}
		//private function key(e:GameSocketEvent == null):void {
		private function key(e:GameSocketEvent) { 
			if (e.key == 65) {//d
				_enfermeiro.px -= 15;
			}
			if (e.key == 68) {//a
				_enfermeiro.px += 15;
			}
			if (e.key == 39) {//right
				_ambulancia.px += 10;
			}
			if (e.key == 37) {//left
				_ambulancia.px -= 10;
			}
		}
	}
	
}